﻿@model IEnumerable<RoutineModel>
@using Citi.Scheduler.WebUi.Models
@using GridMvc.Html
<script type="text/javascript">
    function openUpdateParameterDialog(routineId) {
        //$("#divUpdateParameter").dialog({ modal: true, resizable: false });
        $('#divUpdateParameter').modal('toggle');
        $('#modalUpdateParameter').toggle();

        if (routineId.statusText == "OK") {
            $("#buttonSave").show();
            return;
        }

        $("#routineId").val(routineId);
        $("#buttonSave").hide();

        updateAvailableMachines();
    }

    function closeDialogParameter() {
        $('#divUpdateParameter').modal('toggle');
        //$('.modal').hide();
        $('#modalUpdateParameter').toggle();
        clearForm();
    }

    function openUpdateRoutineDialog() {
        $('#divUpdateRoutine').modal('toggle');
        $('#modalUpdateRoutine').toggle();

        setMasks();
        //$('.modal-backdrop').show();
    }

    function closeDialogRoutine() {
        $('#divUpdateRoutine').modal('toggle');
        $('#modalUpdateRoutine').toggle();
        //$('.modal-backdrop').hide(); 

        $("form").trigger('reset');
    }

    function clearForm() {
        $("#formActionForm").trigger('reset');
    }
</script>
@helper CustomRenderingOfColumn(RoutineModel routine)
    {
    @Ajax.ActionLink("Edit", "ActionForm", new { id = routine.Id },
                new AjaxOptions { UpdateTargetId = "divUpdateRoutine", OnComplete = "openUpdateRoutineDialog" },
        new { @class = "icon-pencil", title = "Edit" })
    <text>|</text>
    @Ajax.ActionLink("Delete", "Delete", new { id = routine.Id },
        new AjaxOptions { UpdateTargetId = "divIndexRoutine", Confirm = "Are you sure?" },
        new { @class = "icon-trash", title = "Delete" })
    <text>|</text>
    @Ajax.ActionLink("Escalations", "Form", "Escalation", new { routineId = routine.Id },
        new AjaxOptions { LoadingElementId = "load-screen", UpdateTargetId = "divEscalationRoutine", OnBegin = "openEscalationRoutineDialog" },
                new { @class = "icon-exclamation-sign", title = "Escalations" })
    <text>|</text>
    @Ajax.ActionLink("Schedule", "Form", "Schedule",
        new { id = routine.RoutineSchedules.Any() ? (long?)routine.RoutineSchedules.OrderBy(x => x.Date).Last().ScheduleId : null },
        new AjaxOptions { UpdateTargetId = "divSchedule", OnBegin = string.Concat("openScheduleDialog('", routine.Id, "');") },
                new { @class = "icon-calendar", title = "Schedule" })
    <text>|</text>
    @Ajax.ActionLink("Parameters", "Index", "RoutineParameter", new { routineId = routine.Id },
        new AjaxOptions { UpdateTargetId = "divUpdateParameter", OnComplete = "openUpdateParameterDialog" },
                 new { @class = "icon-wrench", title = "Parameters" })
    <text>|</text>
    @Ajax.ActionLink("Requisites", "Form", "RoutineRequisite", new { routineId = routine.Id },
        new AjaxOptions { UpdateTargetId = "divUpdateParameter", OnComplete = "openUpdateParameterDialog(" + routine.Id + ")" },
            new { @class = "icon-check", title = "Requisites" })
    <text>|</text>
    @Ajax.ActionLink("See Hierarchy", "RoutineHierarchy", "Routine", new { id = routine.Id },
        new AjaxOptions { UpdateTargetId = "divUpdateParameter", OnComplete = "openUpdateParameterDialog(" + routine.Id + ")" },
            new { @class = "icon-th-list", title = "See Hierarchy" })
    <text>|</text>
    @Ajax.ActionLink("Run now", "Create", "RoutineOnDemand", new { routineId = routine.Id },
    new AjaxOptions
    {
        Confirm = string.Concat("This routine will be executed when some machine becomes available or if the routine has a specific machine, when this machine becomes available. ",
            "This action can not be canceled and if this routine is scheduled to execute today and it hasn't been executed yet, the scheduled routine will be canceled. Do you want to continue?")
    },
    new { @class = "icon-play", title = "Run Now" })
}
@Html.Grid(Model).Columns(columns =>
        {
            columns.Add(c => c.Id, true);
            columns.Add(c => c.Name).Titled("Name").Filterable(true)
                .RenderValueAs(o => string.Format("{0} - {1}", o.Name, o.UseLastVersion ? "Last Version" : o.LibraryVersion.Version));
            columns.Add(c => c.User.Name).Titled("Responsible").Filterable(true)
                .RenderValueAs(o => string.Format("{0} ({1})", o.User.Name, o.User.Soeid));
            columns.Add(c => c.Category.Name).Titled("Category").Filterable(true);
            columns.Add(c => c.Class).Titled("Class");
            columns.Add(o => o.LibraryId)
                .Titled("Actions")
                .SetWidth(200)
                .Encoded(false)
                .Sanitized(false)
                .RenderValueAs(o => CustomRenderingOfColumn(o));
        }).WithPaging(15).Sortable(true)
<div class="modal hide" id="modalUpdateParameter" style="width: 760px;">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" onclick="closeDialogParameter();">
            ×</button>
        <h3>
            Configuration</h3>
    </div>
    <div class="modal-body" style="width: 750px; overflow: hidden">
        <div id="divUpdateParameter" style="height: 80%; width: 100%; overflow: auto">
        </div>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn btn-primary" id="buttonSave" onclick="submitParameterForm();">
            Save</a> <a href="#" class="btn" data-dismiss="modal" onclick="closeDialogParameter();">
                Close</a>
    </div>
</div>
<div class="modal hide" id="modalUpdateRoutine" style="width: 740px;">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" onclick="closeDialogRoutine();">
            ×</button>
        <h3>
            Routine</h3>
    </div>
    <div class="modal-body" id="divUpdateRoutine" style="width: 100%; overflow-y: hidden;">
    </div>
    <div class="modal-footer">
        <a href="#" class="btn btn-primary" onclick="$('#saveRoutine').click();">Save</a>
        <a href="#" class="btn" data-dismiss="modal" onclick="closeDialogRoutine();">Close</a>
    </div>
</div>
